package com.jiangjiesheng.keepappalive;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.jiangjiesheng.keepappalive.utils.Contants;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 运动界面，处理各种保活逻辑
 * <p>
 * Created by jianddongguo on 2017/7/7.
 * http://blog.csdn.net/andrexpert
 */

public class SportsActivity extends AppCompatActivity {
    private static final String TAG = "SportsActivity";
    private Toolbar mToolBar;
    private TextView mTvRunTime;
    private Button mBtnRun;

    private int timeSec;
    private int timeMin;
    private int timeHour;
    private Timer mRunTimer;
    private boolean isRunning;

    KeepAliveManager keepAliveManager =  KeepAliveManager.getInstance(this);


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sports);
        if (Contants.DEBUG)
            Log.d(TAG, "--->onCreate");
        keepAliveManager.init(new KeepAliveManager.KeepAliveManagerStatusListener() {
            @Override
            public void onUserPresent() {
                Toast.makeText(SportsActivity.this, "用户解锁了", Toast.LENGTH_LONG).show();
            }
        });
        initView();
    }

    private void initView() {
        mToolBar = (Toolbar) findViewById(R.id.toolbar_sports);
        mToolBar.setTitleTextColor(getResources().getColor(R.color.colorWhite));
        mToolBar.setTitle("跑步啦");
        mTvRunTime = (TextView) findViewById(R.id.tv_run_time);
        mBtnRun = (Button) findViewById(R.id.btn_run);
    }

    public void onRunningClick(View v) {
        if (!isRunning) {
            mBtnRun.setText("停止跑步(相关于后台，开启保活)");
            startRunTimer();
            keepAliveManager.startKeeper();
        } else {
            mBtnRun.setText("开始跑步(此时相当于前台，关闭保活)");
            stopRunTimer();

            keepAliveManager.stopKeeper();

        }
        isRunning = !isRunning;
    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 禁用返回键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isRunning) {
                Toast.makeText(SportsActivity.this, "正在跑步", Toast.LENGTH_SHORT).show();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private void startRunTimer() {
        TimerTask mTask = new TimerTask() {
            @Override
            public void run() {
                timeSec++;
                if (timeSec == 60) {
                    timeSec = 0;
                    timeMin++;
                }
                if (timeMin == 60) {
                    timeMin = 0;
                    timeHour++;
                }
                if (timeHour == 24) {
                    timeSec = 0;
                    timeMin = 0;
                    timeHour = 0;
                }
                // 更新UI
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mTvRunTime.setText(timeHour + " : " + timeMin + " : " + timeSec);
                    }
                });
            }
        };
        mRunTimer = new Timer();
        // 每隔1s更新一下时间
        mRunTimer.schedule(mTask, 1000, 1000);
    }

    private void stopRunTimer() {
        if (mRunTimer != null) {
            mRunTimer.cancel();
            mRunTimer = null;
        }
        timeSec = 0;
        timeMin = 0;
        timeHour = 0;
        mTvRunTime.setText(timeHour + " : " + timeMin + " : " + timeSec);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (Contants.DEBUG)
            Log.d(TAG, "--->onDestroy");
        stopRunTimer();
        keepAliveManager.stopScreenListenerOnDestroy();
    }
}
